package com.cn.aam.checaiduo.center;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.utils.WindowsUtils;
import com.orhanobut.logger.Logger;

/**
 * @author 艹羊
 * @project Checaiduo_Android
 * @date 2017/6/1 上午9:54
 * @description
 */

public class PopupWindowCenter {

    private PopupWindowCenter() {
    }

    public static PopupWindowCenter getInstance() {
        return PopupWindowCenter.Holder.INSTANCE;
    }

    private static class Holder {
        static PopupWindowCenter INSTANCE = new PopupWindowCenter();

    }

    /**
     * @param context         上下文
     * @param parentView      浮动的主视图
     * @param onClickListener 点击监听者
     * @description 显示产品列表的浮动窗口
     */
    public void showProductListPopupWindow(Context context, View parentView, final View.OnClickListener onClickListener) {
        //设置contentView
        View contentView = LayoutInflater.from(context).inflate(R.layout.popupwindow_product_filter, null);
        final PopupWindow mPopWindow = new PopupWindow(contentView, WindowsUtils.dip2px(128), LinearLayout.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setContentView(contentView);
        mPopWindow.setFocusable(true);
        mPopWindow.setTouchable(true);


        mPopWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(context, R.color.color_transparent)));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mPopWindow.setElevation(10);//必须在设置背景后方能使用
        }
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.fade_in);
        contentView.startAnimation(animation);
//        mPopWindow.showAsDropDown(target);
        mPopWindow.showAtLocation(parentView, Gravity.LEFT | Gravity.TOP, WindowsUtils.dip2px(0), WindowsUtils.dip2px(60));

        //contentView
        //all
        TextView tvAll = (TextView) contentView.findViewById(R.id.tv_all);
        tvAll.setTag("all");
        tvAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopWindow.dismiss();
                if (onClickListener != null)
                    onClickListener.onClick(v);
            }
        });

        TextView rvRaiseFund = (TextView) contentView.findViewById(R.id.tv_raise_fund);
        rvRaiseFund.setTag("raise");
        rvRaiseFund.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopWindow.dismiss();
                if (onClickListener != null)
                    onClickListener.onClick(v);
            }
        });
        TextView rvcarryInterest = (TextView) contentView.findViewById(R.id.tv_carry_interest);
        rvcarryInterest.setTag("carry");
        rvcarryInterest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopWindow.dismiss();
                if (onClickListener != null)
                    onClickListener.onClick(v);
            }
        });
        TextView rvBackedMoney = (TextView) contentView.findViewById(R.id.tv_backed_money);
        rvBackedMoney.setTag("back");
        rvBackedMoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopWindow.dismiss();
                if (onClickListener != null)
                    onClickListener.onClick(v);
            }
        });
    }
}
